package jt.classic.app.bgypgl.bgyp;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.limp.basework.console.XSystemConsole;
import org.limp.basework.console.XSystemConsoleRunException;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.mine.Controller;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;
public class BgypAction extends SimpleAction{
    


    /**
     * 按月统计查询
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list4query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        
        Date date= new Date();
        
        int year=date.getYear()+1900;
        
        int dq_month = date.getMonth()+1;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String name = StringTool.checkString(param.get("NAME"));

        request.setAttribute("NAME", name);
        
        String month = StringTool.checkString(param.get("month"));
        
        request.setAttribute("month", month);
        
        if(!"".equals(month)){
	        if(Integer.parseInt(month)<10){
	        	month = "0"+month;
	        }
        }else{
        	if(dq_month<10){
        		month = "0"+dq_month;
        	}else{
        		month = dq_month+"";
        	}
        	
        }
        
        String start_time = year+month+"01000000";
        String end_time = year+month+"31000000";
        
        /*String userid = StringTool.checkString(param.get("CK_USERID"));

        request.setAttribute("userid", userid);
*/
        IUser user = ISystem.getSessionUser(request);

        StringBuffer where = new StringBuffer(" WHERE CK_DATE>='"+start_time+"' AND " +
        		"CK_DATE<='"+end_time+"'");

        /*if (ISystem.isAdmin(user)) {
            //donothing
        } else {
            if (user.igroup() != null) {
                String gname = user.igroup().ibranchgroup(0);
                String uid = user.id();
                where.append(" AND WJCS.USERID = '" + uid + "' ");
            }
        }*/

        if (!name.equals("")) {
            where.append(" AND NAME='"+name+"'");
        }

        where.append(" GROUP BY NAME");

        String sql1 = "SELECT COUNT(DISTINCT(NAME)) as num FROM APP_BGYP_CK " +
            where.toString();
        String sql2 = "SELECT SUM(CK_NUM) as CK_NUM,NAME,PRICE FROM APP_BGYP_CK " + where.toString();
            //" ORDER BY  WJJS.WJTIME ASC";

        String url = request.getContextPath() + "/app/bgyp.do?method=list4query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // ����
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // �����ת����
            Controller controller = plus.getController("form1");

            // ���õ�ת����
            controller.setType("input");

            // ��ʾ��ǩ
            Label label = plus.getLabel();

            // ������ʾ��ǩ����
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;
            
            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("list4query");
    }
    
    
    /**
     * 维修记录统计
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward listwx_query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        
        Date date= new Date();
        
        int year=date.getYear()+1900;
        
        int dq_month = date.getMonth()+1;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String name = StringTool.checkString(param.get("NAME"));

        request.setAttribute("NAME", name);
        
        String month = StringTool.checkString(param.get("month"));
        
        request.setAttribute("month", month);
        
        if(!"".equals(month)){
	        if(Integer.parseInt(month)<10){
	        	month = "0"+month;
	        }
        }else{
        	
        	month="00";
        }
        
        String start_time = year+month+"01000000";
        String end_time = year+month+"31000000";
        
        /*String userid = StringTool.checkString(param.get("CK_USERID"));

        request.setAttribute("userid", userid);
*/
        IUser user = ISystem.getSessionUser(request);
        StringBuffer where = new StringBuffer();
        if(!"00".equals(month) && !name.equals("")){
        	where.append(" WHERE WX_DATE>='"+start_time+"' AND " +
            		"WX_DATE<='"+end_time+"' AND NAME='"+name+"'");
        }else if (!name.equals("")) {
            where.append(" WHERE NAME='"+name+"'");
        }else if(!"00".equals(month)){
        	where.append(" WHERE WX_DATE>='"+start_time+"' AND " +
            		"WX_DATE<='"+end_time+"'");
        }
        

        /*if (ISystem.isAdmin(user)) {
            //donothing
        } else {
            if (user.igroup() != null) {
                String gname = user.igroup().ibranchgroup(0);
                String uid = user.id();
                where.append(" AND WJCS.USERID = '" + uid + "' ");
            }
        }*/

        

        where.append(" GROUP BY NAME");

        String sql1 = "SELECT COUNT(DISTINCT(NAME)) as num FROM APP_BGYP_WX " +
            where.toString();
        String sql2 = "SELECT SUM(WX_NUM) as WX_NUM,NAME,PRICE FROM APP_BGYP_WX " + where.toString();
            //" ORDER BY  WJJS.WJTIME ASC";

        String url = request.getContextPath() + "/app/bgyp.do?method=listwx_query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // ����
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // �����ת����
            Controller controller = plus.getController("form1");

            // ���õ�ת����
            controller.setType("input");

            // ��ʾ��ǩ
            Label label = plus.getLabel();

            // ������ʾ��ǩ����
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;
            
            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("listwx_query"); 
    }
    
    /**
     * 废弃记录统计
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward listty_query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        
        Date date= new Date();
        
        int year=date.getYear()+1900;
        
        int dq_month = date.getMonth()+1;

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String name = StringTool.checkString(param.get("NAME"));

        request.setAttribute("NAME", name);
        
        String month = StringTool.checkString(param.get("month"));
        
        request.setAttribute("month", month);
        
        if(!"".equals(month)){
	        if(Integer.parseInt(month)<10){
	        	month = "0"+month;
	        }
        }else{
        	
        	month="00";
        }
        
        String start_time = year+month+"01000000";
        String end_time = year+month+"31000000";
        
        /*String userid = StringTool.checkString(param.get("CK_USERID"));

        request.setAttribute("userid", userid);
*/
        IUser user = ISystem.getSessionUser(request);
        StringBuffer where = new StringBuffer("WHERE SFKY='-1' ");
        if(!"00".equals(month) && !name.equals("")){
        	where.append(" AND BG_DATE>='"+start_time+"' AND " +
            		"BG_DATE<='"+end_time+"' AND NAME='"+name+"'");
        }else if (!name.equals("")) {
            where.append(" AND NAME='"+name+"'");
        }else if(!"00".equals(month)){
        	where.append(" AND BG_DATE>='"+start_time+"' AND " +
            		"BG_DATE<='"+end_time+"'");
        }
        

        /*if (ISystem.isAdmin(user)) {
            //donothing
        } else {
            if (user.igroup() != null) {
                String gname = user.igroup().ibranchgroup(0);
                String uid = user.id();
                where.append(" AND WJCS.USERID = '" + uid + "' ");
            }
        }*/

        

        where.append(" GROUP BY NAME");

        String sql1 = "SELECT COUNT(DISTINCT(NAME)) as num FROM APP_BGYP_BM " +
            where.toString();
        String sql2 = "SELECT SUM(KC_NUM) as KC_NUM,NAME,PRICE FROM APP_BGYP_BM " + where.toString();
            //" ORDER BY  WJJS.WJTIME ASC";

        String url = request.getContextPath() + "/app/bgyp.do?method=listty_query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // ����
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // �����ת����
            Controller controller = plus.getController("form1");

            // ���õ�ת����
            controller.setType("input");

            // ��ʾ��ǩ
            Label label = plus.getLabel();

            // ������ʾ��ǩ����
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;
            
            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("listty_query");
    }
    
    
}
